package org.zyl.oss.upload;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

import org.zyl.oss.general.GeneralOssObjectContentTypeEnum;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.ObjectMetadata;

/**
 * 文件上传
 * 
 * @author yzzhouyalei@foxmail.com
 * @time 2017-06-07 09:46:49
 */
public class Upload {
	private OssConfig config;
	public Upload(OssConfig config) {
		this.config = config;
	}
	
	
	public String getNewPathName(String oldFileName) {
		Calendar cal = Calendar.getInstance();
		if(oldFileName.indexOf(".")==-1) {
//			throw new Exception("获取不到文件后缀");
			return cal.getTimeInMillis()+"";
		}
		String fileType = oldFileName.substring(oldFileName.indexOf(".")+1, oldFileName.length());
		
		String pathName = cal.get(Calendar.YEAR)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.DAY_OF_MONTH)+"/";
		String fileName = cal.getTimeInMillis()+"."+fileType;
		return pathName + fileName;
	}
	/**
	 * 上传图片到阿里云
	 * 
	 * @author yzzhouyalei@foxmail.com
	 * @time 2017-03-23 10:03:19
	 */
	public void uploadFile(InputStream is, String pathName) throws Exception {
		try {
			OSSClient client = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());

			System.out.println("正在上传 "+pathName+"...");
			
			client.putObject(config.getBucketName(), pathName, is, getImageMetadata(is.available(), pathName));

			System.out.println("path:" + pathName.replaceAll("\\\\", "/"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void copyFile(String sourcePath,String targetPath) {
		OSSClient client = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
		// 创建CopyObjectRequest对象。
		CopyObjectRequest copyObjectRequest = new CopyObjectRequest(config.getBucketName(), sourcePath, config.getBucketName(), targetPath);

		// 设置新的文件元信息。
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentType(GeneralOssObjectContentTypeEnum.getText(sourcePath.substring(sourcePath.lastIndexOf(".") + 1).toLowerCase()));
		copyObjectRequest.setNewObjectMetadata(meta);

		// 复制文件。
		CopyObjectResult result = client.copyObject(copyObjectRequest);
		System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
	}

	/**
	 * 获取图片属性
	 */
	public static ObjectMetadata getImageMetadata(long fileSize, String fileName) throws IOException {
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(fileSize);
		objectMetadata.setCacheControl("no-cache");
		objectMetadata.setHeader("Pragma", "no-cache");
		objectMetadata.setContentType(GeneralOssObjectContentTypeEnum.getText(fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase()));
		objectMetadata.setContentDisposition("inline;filename=" + fileName);
		return objectMetadata;
	}
}